home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / gamesmaster / source / asm / demos / stars8.s < prev    next >
Encoding:
Text File  |  1996-09-08  |  8.9 KB  |  325 lines

  1. ;3D STARFIELD 8
  2. ;--------------
  3. ;Same as the first starfield demo but runs in 8 colours.  Default is 600
  4. ;stars which runs fine on my A1200+Fast - but try 5000 stars or more for a
  5. ;real visual experience :-)
  6.  
  7.     opt    o+
  8.  
  9.     INCLUDE    "exec/exec_lib.i"
  10.     INCLUDE    "games/games_lib.i"
  11.     INCLUDE    "games/games.i
  12.  
  13. CALL    MACRO
  14.     jsr    _LVO\1(a6)
  15.     ENDM
  16.  
  17. NSTARS    =    600                      ;Number of stars
  18.  
  19. XSPEED    =    -4
  20. YSPEED    =    6
  21. ZSPEED    =    2
  22.  
  23. SCR_HEIGHT =    256
  24. SCR_WIDTH =    320
  25.  
  26.     SECTION    "Stars",CODE
  27.  
  28. ;==========================================================================;
  29. ;                             INITIALISE DEMO
  30. ;==========================================================================;
  31.  
  32. Start:    MOVEM.L    A0-A6/D1-D7,-(SP)
  33.     move.l    ($4).w,a6
  34.     lea    GMS_Name(pc),a1
  35.     moveq    #$00,d0
  36.     CALL    OpenLibrary
  37.     move.l    d0,GMS_Base
  38.     beq    Quit
  39.  
  40.     move.l    GMS_Base(pc),a6
  41.     CALL    SetUserPri
  42.  
  43.     move.l    GMS_Base(pc),a6          ;Tell GMS that we want to add a
  44.     lea    ScreenStruct(pc),a0      ;screen for use.
  45.     CALL    Add_Screen
  46.     tst.l    d0
  47.     bne    Error
  48.  
  49.     lea    ScreenStruct(pc),a0      ;Now show the screen/pic.
  50.     CALL    Show_Screen
  51.  
  52. ;==========================================================================;
  53.  
  54. ;==========================================================================;
  55.  
  56.     ;Randomize star coordinates
  57.  
  58.     lea    StarCoords,a0            ;a0 = Ptr to star co-ordinates.
  59.     move.w    #NSTARS-1,d7
  60. .loop1    move.w    #8192,d1
  61.     CALL    SlowRandom
  62.     move.w    d0,(a0)+
  63.     CALL    SlowRandom
  64.     move.w    d0,(a0)+
  65.     CALL    SlowRandom
  66.     move.w    d0,(a0)+
  67.     dbra    d7,.loop1
  68.  
  69.     ;Construct perspective table
  70.  
  71.     lea    PersTable,a0             ;a0 = ptr to perspective table.
  72.     moveq    #0,d1                    ;d1 = Starting at 0.
  73. .loop2    move.l    #$95FFFF,d2              ;d2 = $95FFFF
  74.     move.l    d1,d3                    ;d3 = d1
  75.     add.w    #300,d3                  ;d3 = ++300
  76.     divu    d3,d2                    ;d2 = ($95ffff)/d3
  77.     move.w    d2,(a0)+                 ;a0 = d2+
  78.     addq.w    #1,d1                    ;d1 = ++1
  79.     cmp.w    #8192,d1                 ;d1 = Equal to 8192?
  80.     bne.s    .loop2
  81.  
  82.     ;Construct plot tables for fast drawing.
  83.  
  84.     lea    PlotXTable,a0            ;a0 = X table - byte positions.
  85.     lea    PlotBTable,a1            ;a1 = Bit table (X related).
  86.     lea    PlotYTable,a2            ;a2 = Y table - line position.
  87.     moveq    #0,d0                    ;d0 = 00
  88. .loop3    move.w    d0,d1                    ;d1 = d0
  89.     lsr.w    #3,d1                    ;d1 = (d0)<<3
  90.     move.w    d1,(a0)+                 ;a0 = d1+
  91.  
  92.     move.w    d0,d1                    ;d1 = d0
  93.     eor.w    #$FFFF,d1                ;d1 = (d0) eor $ffff
  94.     and.w    #%00000111,d1            ;d1 = &%00000111
  95.     move.w    d1,(a1)+                 ;a1 = BitSet++
  96.  
  97.     cmp.w    #SCR_HEIGHT,d0           ;Write out the Y values for the
  98.     bge.s    .plot2                   ;table.
  99.     move.w    d0,d1                    ;d1 = Line Number.
  100.     mulu    #120,d1                  ;d1 = (LineNumber)*80
  101.     move.w    d1,(a2)+                 ;a2 = (LineNumber*80)++
  102.  
  103. .plot2    addq.w    #1,d0
  104.     cmp.w    #SCR_WIDTH,d0
  105.     bne.s    .loop3
  106.  
  107. ;==========================================================================;
  108. ;                                MAIN LOOP
  109. ;==========================================================================;
  110.  
  111. MainLoop:
  112.     move.l    GMS_Base(pc),a6
  113.     CALL    Wait_OSVBL
  114.  
  115.     lea    ScreenStruct(pc),a0
  116.     CALL    SwapBuffers
  117.  
  118. ;==========================================================================;
  119. ;                             STAR ANIMATION
  120. ;==========================================================================;
  121.  
  122.     movem.w    StarXPos(pc),d0/d1/d2    ;MV = d0/d1/d2 = XPos/YPos/ZPos
  123.     add.w    #XSPEED,d0               ;d0 = (StarXPos)+XSPEED
  124.     add.w    #YSPEED,d1               ;d1 = (StarYPos)+YSPEED
  125.     add.w    #ZSPEED,d2               ;d2 = (StarZPos)+ZSPEED
  126.     and.w    #%0000011111111111,d0
  127.     and.w    #%0000011111111111,d1
  128.     and.w    #%0000011111111111,d2
  129.     movem.w    d0/d1/d2,StarXPos
  130.  
  131.     lea    Sinus(pc),a0             ;a0 = Sinus table.
  132.     movem.w    StarXAdd(pc),d3/d4/d5    ;MV = d3/d4/d5 : X/Y/Z
  133.     add.w    (a0,d0.w),d3
  134.     add.w    (a0,d1.w),d4
  135.     add.w    (a0,d2.w),d5
  136.     movem.w    d3/d4/d5,StarXAdd
  137.  
  138. ;===========================================================================;
  139. ;                              SCREEN CLEAR
  140. ;===========================================================================;
  141. ;Let the blitter clear our 3rd screen while the CPU draws the stars.
  142.  
  143. .wait    btst    #6,$dff002
  144.     bne.s    .wait
  145.     move.l    ScreenStruct+SS_MemPtr3(pc),$dff054
  146.     move.w    #0,$dff066
  147.     move.l    #$01000000,$dff040
  148.     move.w    #(SCR_HEIGHT*AMT_PLANES<<6)+SCR_WIDTH/16,$dff058
  149.  
  150. ;==========================================================================;
  151. ;                               DRAW STARS
  152. ;==========================================================================;
  153.  
  154.     lea    StarCoords,a0            ;Draw starfield
  155.     lea    PersTable,a1             ;a1 = Perspective table.
  156.     lea    PlotXTable,a2            ;a2 = X table.
  157.     lea    PlotBTable,a3            ;a3 = Bit table.
  158.     lea    PlotYTable,a4            ;a4 = Y table.
  159.  
  160.     move.l    ScreenStruct+SS_MemPtr2(pc),a6
  161.  
  162.     movem.w    StarXAdd(pc),d3/d4/d5    ;MV = d3/d4/d5 : ?
  163.     add.w    #4096,d3                 ;d3 = ++4096
  164.     add.w    #4096,d4                 ;d4 = ++4096
  165.  
  166.     move.w    #NSTARS-1,d7
  167.  
  168. .draw    movem.w    (a0)+,d0/d1/d2           ;MV = d0/d1/d2 : XPos/YPos/ZPos.
  169.     add.w    d3,d0                    ;Increase XPos.
  170.     and.w    #8191,d0                 ;d0 = And'd
  171.     sub.w    #4096,d0                 ;d0 = --4096
  172.  
  173.     add.w    d4,d1                    ;Y-movement
  174.     and.w    #8191,d1                 ;d1 = And'd
  175.     sub.w    #4096,d1                 ;d1 = --4096
  176.  
  177.     add.w    d5,d2                    ;Z-movement
  178.     and.w    #8191,d2
  179.     add.w    d2,d2                    ;d2 = *2 [word]
  180.     move.w    (a1,d2.w),d6             ;d6 = Read from Perspective table.
  181.  
  182.     muls    d6,d0                    ;X-projection
  183.     swap    d0
  184.     add.w    #176,d0
  185.  
  186.     cmp.w    #SCR_WIDTH-1,d0
  187.     bhi    .nodraw
  188.     muls    d6,d1                    ;Y-projection
  189.     swap    d1
  190.     add.w    #136,d1
  191.     cmp.w    #SCR_HEIGHT-1,d1
  192.     bhi    .nodraw
  193.  
  194.     add.w    d0,d0                    ;d0 = *2 [word]
  195.     add.w    d1,d1                    ;d1 = *2 [word]
  196.     move.w    (a4,d1.w),d6             ;d6 = Plot Y.
  197.     add.w    (a2,d0.w),d6             ;d6 = ++PlotX.
  198.     move.w    (a3,d0.w),d0             ;d0 = BitValue.
  199.  
  200. .draw1    cmp.w    #4000,d2                 ;Now draw the star according to
  201.     bgt.s    .draw2                   ;its position in the Z axis.
  202.     bset    d0,(a6,d6.w)
  203.     dbra    d7,.draw
  204.     bra.s    .done
  205.  
  206. .draw2    cmp.w    #6000,d2
  207.     bgt.s    .draw3
  208.     bset    d0,40(a6,d6.w)
  209.     dbra    d7,.draw
  210.     bra.s    .done
  211.  
  212. .draw3    cmp.w    #8000,d2
  213.     bgt.s    .draw4
  214.     bset    d0,(a6,d6.w)
  215.     bset    d0,40(a6,d6.w)
  216.     dbra    d7,.draw
  217.     bra.s    .done
  218.  
  219. .draw4    cmp.w    #10000,d2
  220.     bgt.s    .draw5
  221.     bset    d0,80(a6,d6.w)
  222.     dbra    d7,.draw
  223.     bra.s    .done
  224.  
  225. .draw5    cmp.w    #12000,d2
  226.     bgt.s    .draw6
  227.     bset    d0,(a6,d6.w)
  228.     bset    d0,80(a6,d6.w)
  229.     dbra    d7,.draw
  230.     bra.s    .done
  231.  
  232. .draw6    cmp.w    #14000,d2
  233.     bgt.s    .draw7
  234.     bset    d0,40(a6,d6.w)
  235.     bset    d0,80(a6,d6.w)
  236.     dbra    d7,.draw
  237.     bra.s    .done
  238.  
  239. .draw7    bset    d0,(a6,d6.w)
  240.     bset    d0,40(a6,d6.w)
  241.     bset    d0,80(a6,d6.w)
  242. .nodraw    dbra    d7,.draw
  243.  
  244. .done    move.l    GMS_Base(pc),a6
  245.     moveq    #JPORT1,d0               ;Read from port 1 (mouse).
  246.     moveq    #JT_ZBXY,d1
  247.     CALL    Read_JoyPort             ;Go get joystick status.
  248.     btst    #MB_LMB,d0
  249.     beq    MainLoop
  250.  
  251. ;===========================================================================;
  252. ;                              RETURN TO DOS
  253. ;===========================================================================;
  254.  
  255. ReturnToDOS:
  256.     move.l    GMS_Base(pc),a6
  257.     lea    ScreenStruct(pc),a0
  258.     CALL    Delete_Screen            ;Give back screen memory etc.
  259. Error:    move.l    GMS_Base(pc),a1
  260.     move.l    ($4).w,a6
  261.     CALL    CloseLibrary
  262. Quit:    MOVEM.L    (SP)+,A0-A6/D1-D7
  263.     moveq    #$00,d0
  264.     rts
  265.  
  266. ;===========================================================================;
  267. ;                                  DATA
  268. ;===========================================================================;
  269.  
  270. GMS_Name:
  271.     dc.b    "games.library",0
  272.     even
  273. GMS_Base:
  274.     dc.l    0
  275.  
  276. AMT_PLANES =    3
  277.  
  278. ScreenStruct:
  279.     dc.l    "GSV1",0
  280.     dc.l    0,0,0            ;Screen_Mem1/2/3
  281.     dc.l    0                ;Screen link.
  282.     dc.l    ScreenPalette    ;Address of screen palette.
  283.     dc.l    0                ;Address of rasterlist.
  284.     dc.l    0                ;Amt of colours in palette.
  285.     dc.w    320,256,320,256  ;Screen & Pic Height/Width.
  286.     dc.w    AMT_PLANES       ;Amt_Planes
  287.     dc.w    0,0              ;Top Of Screen, X/Y
  288.     dc.w    0,0              ;X/Y counters (for scrolling).
  289.     dc.l    TPLBUFFER        ;Special attributes.
  290.     dc.w    LORES            ;Screen mode.
  291.     dc.b    INTERLEAVED      ;Screen type
  292.     dc.b    0                ;Reserved.
  293.     even
  294.  
  295. ScreenPalette:                           ; 124   124   124   124
  296.     dc.w    $0000,$0eee,$0ccc,$0aaa  ;%000, %100, %010, %110
  297.     dc.w    $0888,$0666,$0333,$0222  ;%001, %101, %011, %111
  298.  
  299. ;===========================================================================;
  300. ;                                STAR DATA
  301. ;===========================================================================;
  302.  
  303. StarXAdd dc.w    33                       ;Star stuff
  304. StarYAdd dc.w    12
  305. StarZAdd dc.w    -114
  306.  
  307. StarXPos dc.w    0                        ;Sinus positions
  308. StarYPos dc.w    310
  309. StarZPos dc.w    1280
  310.  
  311.     INCLUDE    "StarSinus.i"
  312.  
  313.     SECTION    Storage,BSS
  314.  
  315. StarCoords
  316.     ds.w    NSTARS*3                 ;Star coordinates
  317.  
  318. PersTable
  319.     ds.w    8192                     ;Perspective table
  320.  
  321. PlotXTable ds.w    SCR_WIDTH                ;Plot tables
  322. PlotBTable ds.w    SCR_WIDTH
  323. PlotYTable ds.w    SCR_HEIGHT
  324.  
  325.